package kg.aibek.projecteuler.p10;

import java.util.ArrayList;

public class Problem27 {
	protected void solve() {
		ArrayList<Integer> list = getPrimes();
		int count = 1;
		int step = 0;

		int A = 0;
		int B = 0;
		for (int b : list) {
			for (int a = -b; a <= b; a++) {
				int count2 = 0;
				for (int n = 0; n < b; n++) {
					if (isPrime(n * n + a * n + b)) {
						count2 = n;
					} else {
						break;
					}
				}
				if (count2 >= count) {
					A = a;
					B = b;
					count = count2;
				}
			}

			System.out.println("Step " + (step++) + " of " + list.size());
		}

		System.out.println("Result: " + count + "  A: " + A + " B: " + B + "  "
				+ (A * B));
	}

	private boolean isPrime(int number) {
		if (number <= 0) {
			return false;
		}
		for (int i = 2; i * i <= number; i++) {
			if (number % i == 0)
				return false;
		}
		return true;
	}

	private ArrayList<Integer> getPrimes() {
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (int i = 3; i < 1000; i++) {
			if (isPrime(i)) {
				list.add(i);
			}
		}
		return list;
	}

	private void doIt() {
		for (int i = 0; i < 997; i++) {
			int a = i * i + i * (-995) + 997;
			if (isPrime(a)) {
				System.out.println(i + " " + a + " is prime");
			} else {
				System.out.println(i);
			}
		}
	}

	public static void main(String[] args) {
		Problem27 p = new Problem27();
		 p.solve();
//		p.doIt();
	}
}
